package com.songlanyun.modules.marketing.groupbuy.controller.api;

import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.entity.GoodsAuthShop;
import com.songlanyun.modules.goods.model.vo.ActivitySkuComItemVo;
import com.songlanyun.modules.goods.model.vo.GoodsSkuComVo;
import com.songlanyun.modules.goods.model.vo.SkuComItemVo;
import com.songlanyun.modules.goods.service.GoodsAuthShopService;
import com.songlanyun.modules.goods.service.GoodsService;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyActivityGoods;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyGoodsSku;
import com.songlanyun.modules.marketing.groupbuy.model.dto.GroupBuyActivityGoodsDTO;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyActivityGoodsVO;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyActivityVO;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyGoodsSkuComVo;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityGoodsService;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityService;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyGoodsSkuService;
import com.songlanyun.modules.marketing.seckill.entity.SeckillActivityGoods;
import com.songlanyun.modules.marketing.seckill.entity.SeckillGoodsSku;
import com.songlanyun.modules.marketing.seckill.model.vo.SeckillActivityGoodsVO;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 拼团活动商品表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-27 14:03:38
 */
@RestController
@RequestMapping("api/groupbuy/activitygoods")
public class GroupBuyActivityGoodsApiController extends AbstractController {
    @Autowired
    private GroupBuyActivityGoodsService groupBuyActivityGoodsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsAuthShopService goodsAuthShopService;

    /**
     * 查询该店铺中开始的拼团活动商品
     */
    @GetMapping("/groupbuying/{shopId}")
    public R groupBuying(@PathVariable("shopId") Long shopId) {

        GroupBuyActivityVO vo = groupBuyActivityGoodsService.groupBuying(shopId);

        return R.ok().put("data", vo);
    }

    /**
     * 查询商家某个拼团活动商品列表
     *
     * @param groupBuyId 拼团活动id
     * @return
     */
    @GetMapping("/getGroupBuyGoodsList/{groupBuyId}")
    public R getGroupBuyGoodsList(@PathVariable("groupBuyId") Long groupBuyId) {
        List<GroupBuyActivityGoods> list = groupBuyActivityGoodsService.getGoodsListBuyGroupBuyId(groupBuyId);

        return R.ok().put("list", list);
    }


    /**
     * 查询团商品的详情
     *
     * @param id 拼团活动商品id
     * @return
     */
    @GetMapping("/goodsInfo/{id}")
    public R goodsInfo(@PathVariable("id") Long id) {
        GroupBuyActivityGoodsVO goods = groupBuyActivityGoodsService.getGoodsInfo(id, true);

        return R.ok().put("data", goods);
    }

    /**
     * 这里会涉及到
     * 商品封面图片
     * 商品售价价格（最低，最高）
     * 库存（总库存，与规格库存）
     * 商品规格表
     * 商品规格项
     * 商品当前限购
     *
     * @param groupBuyGoodsId
     * @return
     */
    @GetMapping("/skus/{groupBuyGoodsId}")
    public R getSeckillGoodsSkuList(@PathVariable("groupBuyGoodsId") Long groupBuyGoodsId) {
        GroupBuyActivityGoods groupBuyActivityGoods = groupBuyActivityGoodsService.getById(groupBuyGoodsId);
        Goods goods = goodsService.getById(groupBuyActivityGoods.getGoodsId());

        GroupBuyGoodsSkuComVo goodsSkuComVo = new GroupBuyGoodsSkuComVo();
        goodsSkuComVo.setGoods(groupBuyActivityGoods);
        goodsSkuComVo.setCurrentShopId(goods.getShopId());

        //获得商品的sku集合给移动端
        List<ActivitySkuComItemVo> list = groupBuyActivityGoods.loadResidualInventory();

        goodsSkuComVo.setGoodsSkuList(list);
        //活动商品剩余总库存
        goodsSkuComVo.setTotalStock(groupBuyActivityGoods.getResidueStockTotal());
        //商品剩余总库存
        goodsSkuComVo.setGoodsTotalStock(groupBuyActivityGoods.getResidualInventory());


        /**
         * 获取经营权限
         * 可以获知，1是否是代锁，2，是否上架，3，是否审核通过
         */

        GoodsAuthShop auth = goodsAuthShopService.getAuthorizeInfo(goodsSkuComVo.getCurrentShopId(), groupBuyActivityGoods.getGoodsId());
        goodsSkuComVo.setGoodsAuth(auth);


        return R.ok().put("data", goodsSkuComVo);
    }


    /**
     * APP --   查询今日必拼  -- 此处查询所有的拼团商品（包括各个商家的），所有查询不到结束时间
     */
    @GetMapping("/will-you-spell")
    public R willYouSpell(@RequestParam Map<String,Object> params) {
        List<GroupBuyActivityGoodsVO> list = groupBuyActivityGoodsService.willYouSpell(params);
        return R.ok().put("list", list);
    }

    /**
     * APP --   查询今日必拼 列表 -- 此处查询所有的拼团商品（包括各个商家的），所有查询不到结束时间
     */
    @GetMapping("/will-you-spell-page")
    public R willYouSpellPage(@RequestParam Map<String,Object> params) {
        PageUtils page = groupBuyActivityGoodsService.willYouSpellPage(params);
        return R.ok().put("page", page);
    }

}
